﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace ServiceBase
{
    public static class Split
    {
        /// <summary>
        /// 根据指定的列号将图片切割为两块
        /// </summary>
        /// <param name="bmp"></param>
        /// <param name="xIndex"></param>
        /// <returns></returns>
        public static Bitmap[] ByXIndex(Bitmap bmp, int xIndex)
        {
            Bitmap map1 = bmp.Clone(new Rectangle(0, 0, xIndex, bmp.Height), System.Drawing.Imaging.PixelFormat.Undefined);
            Bitmap map2 = bmp.Clone(new Rectangle(xIndex, 0, bmp.Width - xIndex, bmp.Height), System.Drawing.Imaging.PixelFormat.Undefined);
            Bitmap[] list = new Bitmap[] { map1,map2};
            return list;
        }
        /// <summary>
        /// 按照固定宽度将图片切割为若干块。
        /// </summary>
        /// <param name="bmp"></param>
        /// <param name="width"></param>
        /// <returns></returns>
        public static Bitmap[] ByWidth(Bitmap bmp, int width)
        {
            if (bmp.Width < width)
            {
                width = bmp.Width;
            }
            int count = 1;
            if (bmp.Width % (double)width == 0)
            {
                count = bmp.Width / width;
            }
            else
            {
                count = bmp.Width / width + 1;
            }
            List<Bitmap> list = new List<Bitmap>();
            for (int i = 0; i < count; i++)
            {
                int currWidth;
                if (i == count - 1)
                {
                    currWidth = bmp.Width - (list.Count * width);
                }
                else
                {
                    currWidth = width;
                }
                Bitmap temp = bmp.Clone(new Rectangle(i * width, 0, currWidth, bmp.Height), System.Drawing.Imaging.PixelFormat.Undefined);
                list.Add(temp);
            }
            return list.ToArray();
        }
        /// <summary>
        /// 将超宽的图片切割为若干块。
        /// </summary>
        /// <param name="bmp"></param>
        /// <param name="width"></param>
        /// <returns></returns>
        public static Bitmap[] ByLimitWidth(Bitmap[] list,int limitWidth,int width)
        {
            List<Bitmap> temp = new List<Bitmap>();
            foreach (Bitmap bmp in list)
            {
                if (bmp.Width > limitWidth)
                {
                    foreach (Bitmap item in ByWidth(bmp, width))
                    {
                        temp.Add(item);
                    }
                }
                else
                {
                    temp.Add(bmp);
                }
            }
            return temp.ToArray();
        }
        public static Bitmap[] ByEmptyColumn(Bitmap bmp)
        {


            List<Bitmap> PicList = new List<Bitmap>();
            Rectangle cloneRect;
            int posx1 = bmp.Width, posy1 = bmp.Height, posx2 = 0, posy2 = 0;
            bool cut = false;
            int last = -1, lastx = 0;
            for (int j = 0; j < bmp.Width; j++)      //找有效区
            {
                cut = false;
                for (int i = 0; i < bmp.Height; i++)
                {
                    if (!BaseFunction.IsEmptyPixel(bmp, j, i))//如果是非空白像素
                    {
                        if (posx1 > j) posx1 = j;
                        if (posy1 > i) posy1 = i;
                        if (posx2 < j) posx2 = j;
                        if (posy2 < i) posy2 = i;
                        cut = true;
                    }
                };
                if (cut)
                    if (j != bmp.Width - 1)
                    {
                        continue;
                    }

                if (last + 1 == j)
                {
                    last++;
                    continue;
                }
                cloneRect = new Rectangle(posx1, posy1, posx2 - posx1 + 1, posy2 - posy1 + 1);
                lastx = j;
                last = j;
                posx1 = bmp.Width;
                posy1 = bmp.Height;
                posx2 = 0;
                posy2 = 0;
                PicList.Add(bmp.Clone(cloneRect, bmp.PixelFormat));//复制小块图
            };
            return PicList.ToArray();

        }
        /// <summary>
        /// 使用灰度值判断是否为有效像素进行的切割
        /// </summary>
        /// <param name="bmp"></param>
        /// <returns></returns>
        public static Bitmap[] ByGray(Bitmap bmp)
        {
            List<Bitmap> PicList = new List<Bitmap>();
            Rectangle cloneRect;
            int posx1 = bmp.Width, posy1 = bmp.Height, posx2 = 0, posy2 = 0;
            bool cut = false;
            int last = -1, lastx = 0;
            for (int j = 0; j < bmp.Width; j++)      //找有效区
            {
                cut = false;
                for (int i = 0; i < bmp.Height; i++)
                {
                    int pixelValue = bmp.GetPixel(j, i).R;
                    if (pixelValue < 128)     //根据灰度值
                    {
                        if (posx1 > j) posx1 = j;
                        if (posy1 > i) posy1 = i;
                        if (posx2 < j) posx2 = j;
                        if (posy2 < i) posy2 = i;
                        cut = true;
                    }
                };
                if (cut)
                    if (j != bmp.Width - 1)
                    {
                        continue;
                    }

                if (last + 1 == j)
                {
                    last++;
                    continue;
                }
                cloneRect = new Rectangle(posx1, posy1, posx2 - posx1 + 1, posy2 - posy1 + 1);
                lastx = j;
                last = j;
                posx1 = bmp.Width;
                posy1 = bmp.Height;
                posx2 = 0;
                posy2 = 0;
                PicList.Add(bmp.Clone(cloneRect, bmp.PixelFormat));//复制小块图
            };
            return PicList.ToArray();
        }

    }
}
